<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 系统功能管理
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: system.php 1 2014-01-13 15:05:51Z zhouyl $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Controller_Admin_System extends Controller_Admin_Base
{

    /**
     * 配置设置
     */
    public function action_config()
    {
        $this->privilege('system.config');

        if ($this->request->is_post())
        {
            $post = Validation::factory($this->request->post())
                ->rule('site_name', 'not_empty')
                ->rule('site_title', 'not_empty')
                ->rule('site_url', 'not_empty')
                ->rule('site_url', 'url')
                ->rule('site_resource', 'not_empty')
                ->rule('site_resource', 'url');
            $errors = array();
            if ($post->check())
            {
                foreach ($post->as_array() as $field => $value)
                {
                    preg_match('/^([a-z]+)_(.*)$/', $field, $matches);
                    list(, $group, $key) = $matches;
                    try
                    {
                        Config::set($group, $key, $value);
                    }
                    catch (Exception $e)
                    {
                        $errors[$field] = $e->getMessage();
                    }
                }
                $this->view->success = __('admin.system.success');
            }
            $this->view->errors = array_merge($post->errors('admin.system'), $errors);
            $this->render('admin/common/message');
        }
        $this->view->site = Config::get('site');
    }

}